package com.example.liang.basamovie.myview;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import com.example.liang.basamovie.R;
import com.example.liang.basamovie.activity.WebBannerActivity;
import com.example.liang.basamovie.utils.ImageLoaderUtils;
import java.util.ArrayList;
import java.util.List;
public class MyBannerView extends RelativeLayout {
    ViewPager viewPager;
    RadioGroup radioGroup;
    static List<String> data1;
    static List<String> data2;
    static List<String> data3;
    static MyViewPagerAdapter adapter;
    final List<Bitmap> bit_list=new ArrayList<>();
    public MyBannerView(Context context) {
        super(context);
        init(context,null,0);
    }

    public MyBannerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs,0);
    }

    public MyBannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs,0);
    }

    private void init(Context context, AttributeSet attrs, int i) {
        getlayout(context);
        adapter=new MyViewPagerAdapter();
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(0,false);
        ((RadioButton)(radioGroup.getChildAt(0))).setChecked(true);
        listener lis=new listener();
        viewPager.addOnPageChangeListener(lis);
        radioGroup.setOnCheckedChangeListener(lis);
        automation();
    }
    public static void setData(List<String> data){
        data1=data;
        adapter.notifyDataSetChanged();
    }
    public static void setData1(List<String> data){
        data2=data;
        adapter.notifyDataSetChanged();
    }
    public static void setData2(List<String> data){
        data3=data;
        adapter.notifyDataSetChanged();
    }
    private void automation() {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentItem(viewPager.getCurrentItem()+1);
                postDelayed(this,2000);
            }
        },2000);
    }

    private void getlayout(Context context) {
        viewPager=new ViewPager(context);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        viewPager.setLayoutParams(params);
        addView(viewPager);
        radioGroup=new RadioGroup(context);
        radioGroup.setOrientation(LinearLayout.HORIZONTAL);
        params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.setMargins(0,0,0,20);
        params.addRule(CENTER_HORIZONTAL,TRUE);
        params.addRule(ALIGN_PARENT_BOTTOM,TRUE);
        radioGroup.setLayoutParams(params);
        addView(radioGroup);
        RadioButton radioButton=null;
        RadioGroup.LayoutParams params1=new RadioGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params1.setMargins(10,10,10,10);
        for (int i = 0; i < 5; i++) {
            radioButton=new RadioButton(context);
            radioButton.setLayoutParams(params1);
            radioButton.setButtonDrawable(R.drawable.rbselector);
            radioGroup.addView(radioButton);
        }

    }
public class MyViewPagerAdapter extends PagerAdapter {
    MyViewPagerAdapter(){}
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        ImageView imageView=new ImageView(getContext());
        imageView.setAdjustViewBounds(true);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        if(bit_list.size()!=data1.size()){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    bit_list.clear();
                    for (int i = 0; i < data1.size(); i++) {
                        Bitmap bitmap = ImageLoaderUtils.netPicToBmp(data1.get(i));
                        bit_list.add(bitmap);
                    }
                }
            }).start();
        }
        boolean flag=true;
        while(flag){
            if(data1.size()==bit_list.size()){
                imageView.setImageBitmap(bit_list.get(position %data1.size()));
                flag=false;
            }
        }
        container.addView(imageView);
        imageView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getContext(), WebBannerActivity.class);
                intent.putExtra("url",data1.get(position%5));
                intent.putExtra("maintitle",data2.get(position%5));
                intent.putExtra("subtitle",data3.get(position%5));
                getContext().startActivity(intent);
            }
        });

        return imageView;
    }

    @Override
    public int getCount() {
        return 5*10000;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
class listener extends ViewPager.SimpleOnPageChangeListener implements RadioGroup.OnCheckedChangeListener{
    int p=0;
    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        for (int m = 0; m < p%5; m++) {
            if(((RadioButton)(radioGroup.getChildAt(m))).isChecked()){
                viewPager.setCurrentItem(m,false);
            }
        }
    }
    @Override
    public void onPageSelected(int position) {
       p=position;
        ((RadioButton)(radioGroup.getChildAt(position%5))).setChecked(true);
    }
}
}
